package com.group8.ge;

import java.util.Vector;

import com.group8.gm.GameController;

public class MapManager  implements Runnable{
	private int pCount;
	private Map map;
	private GameController gc;
	public MapManager(GameController gc){
		this.gc = gc;
		map = new Map(this, 15);
		
	}
	
	public void deleteFromUI(GameObject obj) {
		gc.removeFromUI(obj);
	}
	
	public GameController getGC(){
		return gc;
	}

	/* (non-Javadoc)
	 * @see java.lang.Runnable#run()
	 */
	@Override
	public void run() {
		while(true){
			if(gc.getNoodleManager().getNoodle().getY()<640 && !gc.collisionCheckWithEnemies(gc.getNoodleManager().getNoodle(), map.getEnemiesAsO())){
				
				if(gc.getNoodleManager().getNoodle().getHop_dist() > 150)
					gc.getNoodleManager().getNoodle().setHop_dist(150);
				
				if(gc.collisionCheckWithBonuses(gc.getNoodleManager().getNoodle(), map.getBonusesAsO())){
					System.out.println("collision with Bonus :" + map.getBonuses().firstElement().getType());
					gc.getNoodleManager().getNoodle().applyBonus(map.getBonuses().firstElement());
					map.removeFromCanvas(map.getBonuses().firstElement());
				}
				map.generatePlates();
				if(gc.getScore() % 165 == 0 && map.getEnemies().size() <1){
					map.generateEnemy();
				}
				if(gc.getScore() % 135 == 0 && map.getBonuses().size() <1){
					map.generateBonus(map.getPlates().lastElement());
				}
				if(map.getBonuses().size() == 1 && map.getBonuses().firstElement().getY() > 610){
					map.getBonuses().removeAllElements();
				}
				map.goDown();
				try {
					Thread.sleep(5);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				map.checkAndRemove();
				try {
					Thread.sleep(5);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				gc.updateUI();
				try {
					Thread.sleep(10);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				for(Plate p: getMap().getPlates())
					gc.getGamePanel().addObject(p);
				for(Enemy e: map.getEnemies()){
					e.move();
				}
				for(Enemy e: map.getEnemies())
					gc.getGamePanel().addObject(e);
				for(Bonus e: map.getBonuses())
					gc.getGamePanel().addObject(e);
			}
			else
				gc.endGame();
		}
		
	}
	public Map getMap(){
		return map;
	}
	
	public Vector<GameObject> getDownNoodle(double y){
		return map.getDownNoodle(y);
	}
	
}
